package io.gitee.zhangbinhub.acp.cloud.helloworld.controller

import cn.dev33.satoken.annotation.SaCheckPermission
import cn.dev33.satoken.annotation.SaCheckRole
import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.cloud.helloworld.nami.Hello
import io.gitee.zhangbinhub.acp.cloud.helloworld.nami.World
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.noear.nami.annotation.NamiClient
import org.noear.solon.annotation.*
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.Valid

@Api("控制器")
@Valid
@Controller
open class TestController(private val logAdapter: LogAdapter) {
    @NamiClient
    private var hello: Hello? = null

    @NamiClient
    private var world: World? = null

    @Inject("\${acp.test.properties}", autoRefreshed = true)
    private var properties: String? = null

    @ApiOperation("test-helloworld")
    @Post
    @Consumes(MimeType.TEXT_PLAIN_VALUE)
    @Produces(MimeType.TEXT_PLAIN_VALUE)
    @SaCheckRole("ADMIN")
    @Mapping("/helloworld")
    fun helloWorld(@Body content: String): String {
        val response = (hello?.hello(content) ?: " hystrix ") + ";" +
                (world?.world(content) ?: " hystrix ") + ";" +
                "properties=$properties"
        logAdapter.info(response)
        return response
    }

    @ApiOperation("test-exception")
    @Post
    @Mapping("/exception")
    @SaCheckPermission("system1")
    @Throws(WebException::class)
    fun exception(): String {
        logAdapter.info("test-exception")
        throw WebException("test exception!")
    }
}